<template>
  <div>
      <div class="title">
        注册界面
        <hr>
      </div>
      <el-row type="flex">
        <el-col :span="8"></el-col>
        <el-col :span="8">
                <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pwd">
                <el-input v-model="ruleForm.pwd" show-password></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="repwd">
              <el-input v-model="ruleForm.repwd" show-password></el-input>
            </el-form-item>
            <el-form-item label="选择头像" prop="faceImg">
                <div class="demo-fit">
                  <div class="block">
                      <el-avatar shape="circle" :size="150" :src="src"></el-avatar>
                  </div>
                </div>
                <input style="margin-top:8px" type="file"  @change="getFile($event)">
            </el-form-item>
            <el-form-item>
                <el-button type="info" icon="el-icon-arrow-left" size="small" @click="$router.push('/check')">去登录</el-button>
                <el-button  type="primary" icon="el-icon-search" size="small" @click="submitForm('ruleForm')">注册</el-button>
                <el-button type="danger" size="small" @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
      </div>
        </el-col>
        <el-col :span="8"></el-col>
      </el-row>

  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  data(){
    return{
        src:"",
        ruleForm: {
          name: '',
          pwd: '',
          repwd:'',
          faceImg:''
        },
         rules: {
          name: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          pwd: [
            { required: true, message: '请输入密码', trigger: 'change' },
            {min : 5, message: '密码最少需要5位', trigger: 'blur'}
          ],
          // tel: [
          //   { required: true, message: '请输入电话号码', trigger: 'change' },
          //   { pattern: /^1[35789]\d{9,}$/, message: '电话必须是13、15、17、18、19开始的11位数', trigger: 'blur' }
          // ],
          // nickName: [
          //   { required: true, message: '请输入邮箱', trigger: 'change' },
          //   { pattern: /^[a-zA-Z\u4e00-\u9fa5]{2,}$/, message: '昵称必须是两位以上的汉字或字母', trigger: 'blur' }
          // ],
          // realName: [
          //   { required: true, message: '请输入真实姓名', trigger: 'change' },
          //   { pattern: /^[a-zA-Z\u4e00-\u9fa5]{2,}$/, message: '真实姓名必须是两位以上的汉字或字母', trigger: 'blur' }
          // ],
        }
    }
  },
  computed:{
    ...mapState("login",["userBean"]),

  },
   methods: {
    // ...mapMutations("login",["setCustomerAdd"]),
    getFile(event){
      this.filePreview(event);
      //得到文件表单的form对象
      this.ruleForm.file = event.target.files[0];
      //阻止事件默认行为
      event.preventDefault();
    },
    filePreview(e){
      let _this = this
      var files = e.target.files[0]
      if (!e || !window.FileReader) return  // 判断是否支持FileReader
      let reader = new FileReader()
      reader.readAsDataURL(files) // 文件转换
      reader.onloadend = function () {
        _this.src = this.result
        _this.textHide=false;
      }
    },
    async register(){
      var info = await this.ajax.uploadSubmit("/project/user/add",this.ruleForm);
      if(info == "ok"){
        // this.setCustomerAdd(false);
        this.open2();
        this.$router.push("/check");
      } else {
        this.open4();
      }
    },
    open2() {
        this.$message({
          message: '注册成功',
          type: 'success'
        });
    },
    open4() {
        this.$message.error('注册失败');
    },
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.register();
          } else {
            return false;
          }
        });
    },
    resetForm(formName) {
        this.$refs[formName].resetFields();
        this.src = '';
    }

  }

}
</script>

<style>
  .title{
    text-align: center;
    font-size: 24px;
  }
</style>